﻿/**
 * Copyright (c) 2008 Justin Etheredge http://www.codethinked.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Microsoft Public License (Ms-PL)
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/ms-pl.html
 * If redistributing this code, this entire header must remain intact.
 * 
 * Some of the code in this application has been taken 
 * from the following sources (all of which are licensed under
 * the Ms-PL):
 * Matt Warren's Blog: http://blogs.msdn.com/mattwar/
 * MSDN: http://msdn2.microsoft.com/en-us/library/bb546158.aspx
 */

using LinqToSimpleDB;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace LinqToSimpleDBTest
{
    
    
    /// <summary>
    ///This is a test class for SimpleDBContextTest and is intended
    ///to contain all SimpleDBContextTest Unit Tests
    ///</summary>
  [TestClass()]
  public class SimpleDBContextTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    /// <summary>
    ///A test for SimpleDBContext Constructor
    ///</summary>
    [TestMethod()]
    public void SimpleDBContextConstructorTest()
    {
      SimpleDBContext.SetAccessKeys("test", "test");
      SimpleDBContext context = SimpleDBContext.GetContext("test");
      Assert.IsTrue(context.DomainName == "test");
      Assert.IsTrue(context.Precision == 40);
      Assert.IsTrue(context.Scale == 10);
      Assert.IsNotNull(context.Domain);
      Assert.IsNotNull(context.SimpleDBClient);

      context = SimpleDBContext.GetContext("test", 22, 12);
      Assert.IsTrue(context.DomainName == "test");
      Assert.IsTrue(context.Precision == 22);
      Assert.IsTrue(context.Scale == 12);
      Assert.IsNotNull(context.Domain);
      Assert.IsNotNull(context.SimpleDBClient);

      context = SimpleDBContext
        .GetContext("accessKeyId", "secretAccessKey", "test", 22, 12);
      Assert.IsTrue(context.DomainName == "test");
      Assert.IsTrue(context.Precision == 22);
      Assert.IsTrue(context.Scale == 12);
      Assert.IsTrue(context.AccessKeyId == "accessKeyId");
      Assert.IsTrue(context.SecretAccessKey == "secretAccessKey");
      Assert.IsNotNull(context.Domain);
      Assert.IsNotNull(context.SimpleDBClient);
    }

    /// <summary>
    ///A test for SimpleDBContext Constructor
    ///</summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void SimpleDBContextNullAccessKeyConstructorTest()
    {
      SimpleDBContext context = SimpleDBContext
        .GetContext(string.Empty, string.Empty, string.Empty, 0, 0);
    }

    /// <summary>
    ///A test for SimpleDBContext Constructor
    ///</summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void SimpleDBContextNullSecretKeyConstructorTest()
    {
      SimpleDBContext context = SimpleDBContext
        .GetContext("Test", string.Empty, string.Empty, 0, 0);
    }

    /// <summary>
    ///A test for SimpleDBContext Constructor
    ///</summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void SimpleDBContextNullDomainConstructorTest()
    {
      SimpleDBContext context = SimpleDBContext
        .GetContext("Test", "Test", string.Empty, 0, 0);
    }

    /// <summary>
    ///A test for SimpleDBContext Constructor
    ///</summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentException))]
    public void SimpleDBContextZeroPrecisionConstructorTest()
    {
      SimpleDBContext context = SimpleDBContext
        .GetContext("Test", "Test", "Test", 0, 0);
    }

    /// <summary>
    ///A test for SimpleDBContext Constructor
    ///</summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentException))]
    public void SimpleDBContextZeroScaleConstructorTest()
    {
      SimpleDBContext context = SimpleDBContext
        .GetContext("Test", "Test", "Test", 5, -1);
    }

    [TestMethod()]
    [ExpectedException(typeof(InvalidOperationException))]
    public void GetContextAccessKeyFailureTest()
    {
      string lastAccessKey = SimpleDBContext.StaticAccessKeyId;
      string lastSecretKey = SimpleDBContext.StaticSecretAccessKey;

      SimpleDBContext.SetAccessKeys(string.Empty, string.Empty);
      try
      {
        SimpleDBContext.ListDomains();
      }
      finally
      {
        SimpleDBContext.SetAccessKeys(lastAccessKey, lastSecretKey);
      }
    }

    [TestMethod()]
    [ExpectedException(typeof(InvalidOperationException))]
    public void GetContextSecretKeyFailureTest()
    {
      string lastAccessKey = SimpleDBContext.StaticAccessKeyId;
      string lastSecretKey = SimpleDBContext.StaticSecretAccessKey;

      SimpleDBContext.SetAccessKeys("test", string.Empty);
      try
      {
        SimpleDBContext.ListDomains();
      }
      finally
      {
        SimpleDBContext.SetAccessKeys(lastAccessKey, lastSecretKey);
      }
    }
  }
}
